package com.dmz.common.utils.sftp;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

/**
 * @author lee
 */
@Slf4j
public class SftpUtil {

    public static boolean priKeyConnect(String ftpUserName, String ftpIp, int port,String priKeyFile,String passphrase,String downloadFile,
                                        String saveFile, String fileName){
        boolean result = true;

        ChannelSftp sftp = null;
        Session session = null;
        Channel channel = null;
        JSch jsch=new JSch();
        try {
            if(priKeyFile !=null && !"".equals(priKeyFile)){
                if(passphrase !=null && !"".equals(passphrase)){
                    jsch.addIdentity(priKeyFile, passphrase);
                }else{
                    jsch.addIdentity(priKeyFile);
                }
            }
            if(port >0){
                session=jsch.getSession(ftpUserName, ftpIp, port);
            }else{
                session=jsch.getSession(ftpUserName, ftpIp);
            }
            Properties config=new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
//	       session.setTimeout(FTP_DEFAULT_TIMEOUT_SECOND);
            session.connect(300000);
            channel=session.openChannel("sftp");
            channel.connect(1000);
            sftp = (ChannelSftp)channel;
            System.out.println("通道建立成功！");
        } catch (Exception e) {
            e.printStackTrace();

            result = false;
        }

        if(result){
            try {
                File srcFile = new File(saveFile+File.separator+fileName);
                if(!srcFile.exists()){
                    (new File(srcFile.getParent())).mkdirs();
                }
                sftp.cd(downloadFile);//进入下载地址
//               sftp.dir
                sftp.get(fileName+".gz",saveFile);//目标文件和保存地址
            }catch (Exception e){
                System.out.println(e);
                result = false;
            }finally {
                session.disconnect();
                channel.disconnect();
                sftp.disconnect();
            }
        }
        return result;
    }
    //上传
    public static boolean priKeyUpload(String ftpUserName, String ftpIp, int port,String priKeyFile,String passphrase,String uploadFile,
                                       String sumFile,String fileName){
        boolean result1 = true;
        ChannelSftp sftp = null;
        Session session = null;
        Channel channel = null;
        JSch jsch=new JSch();
        try{
            if(priKeyFile !=null && !"".equals(priKeyFile)){
                if(passphrase !=null && !"".equals(passphrase)){
                    jsch.addIdentity(priKeyFile, passphrase);
                }else{
                    jsch.addIdentity(priKeyFile);
                }
            }
            if(port >0){
                session=jsch.getSession(ftpUserName, ftpIp, port);
            }else{
                session=jsch.getSession(ftpUserName, ftpIp);
            }
            Properties config=new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
//		       session.setTimeout(FTP_DEFAULT_TIMEOUT_SECOND);
            session.connect(300000);
            channel=session.openChannel("sftp");
            channel.connect(1000);
            sftp = (ChannelSftp)channel;
            System.out.println("通道建立成功！");
        }catch(Exception e){
            e.printStackTrace();
            result1 = false;
        }
        if(result1){
            try {
                File srcFile = new File(sumFile+File.separator+fileName+".gz");

                FileInputStream filein = new FileInputStream(srcFile);
                sftp.cd(uploadFile);//进入上传地址

                System.out.println(srcFile.getName());
                sftp.put(filein,srcFile.getName());
            }catch (Exception e){
                e.printStackTrace();

                result1 = true;
            }finally {
                session.disconnect();
                channel.disconnect();
                sftp.disconnect();
            }
        }
        return result1;
    }

}